<?php
	session_start();
	require("connect.php");
	$link = connect_APO();
	require("check.php");
	is_admin();
	
	$midweek_name_old = $_POST['midweekOld'];
	$midweek_name_new = $_POST['midweekTName'];
	$midweek_day = $_POST['midweekTDay'];
	$midweek_start_hr = $_POST['midweekTSTimeHR'];
	$midweek_start_min = $_POST['midweekTSTimeMIN'];
	$midweek_start_m = $_POST['midweekTSTimeM'];
	$midweek_end_hr = $_POST['midweekTETimeHR'];
	$midweek_end_min = $_POST['midweekTETimeMIN'];
	$midweek_end_m = $_POST['midweekTETimeM'];
	$midweek_credit = $_POST['midweekTCredit'];
	$midweek_leader1 = $_POST['midweekTLeader1'];
	$midweek_leader2 = $_POST['midweekTLeader2'];
	$midweek_max_slots = $_POST['midweekTMSlots'];
	$midweek_meeting = $_POST['midweekTMeet'];
	$midweek_location = $_POST['midweekTLocation'];
	$midweek_name_show = $midweek_name_new;
	
	$dontadd = 0;
	
	$midweek_name_new = str_replace(" ", "", $midweek_name_new);
	
	if($midweek_start_hr > 12 || $midweek_end_hr > 12){
		$dontadd = 1;
		$message1 = 'Please keep the hours between 1 and 12';
	}	
	
	if($midweek_start_min > 59 || $midweek_end_min > 59){
		$dontadd = 1;
		$message2 = 'Please keep the minutes between 0 and 59';
	}
	
	if($midweek_start_m == 'PM' && $midweek_start_hr != '12'){
		$midweek_start_hr += 12;
	}
	if($midweek_end_m == 'PM'){
		$midweek_end_hr += 12;
	}
	
	if($midweek_end_hr < $midweek_start_hr || ($midweek_start_hr == $midweek_end_hr && $midweek_start_min > $midweek_end_min)){
		$dontadd = 1;
		$message3 = 'The start time was greater than the end time.';
	} else if($midweek_end_hr == $midweek_start_hr && $midweek_start_min == $midweek_end_min){
		$dontadd = 1;
		$message3 = 'The start and end time were at the same time.';
	}
	
	require("header+logoutB.php");
	headerL();
	if($dontadd == 0){
		$sql = 'Begin;';
		$result = mysql_query($sql);
		$sql = sprintf("CREATE TABLE `%s` (
					  `Name` varchar(25) NOT NULL,
					  `ID` varchar(10) NOT NULL,
					  `Drive` tinyint(1) NOT NULL,
					  PRIMARY KEY  (`Name`,`ID`)
				) ENGINE=InnoDB DEFAULT CHARSET=latin1;", mysql_real_escape_string($midweek_name_new."_".$midweek_day."_".$midweek_start_hr));
		$result = mysql_query($sql);
		if(!$result){
			$error = 1;
			$message = 'There was an error in your query, perhaps that midweek already exists at the same time.';
			$sql = 'ROLLBACK;';
			$result = mysql_query($sql);
		} else{
			$sql = sprintf("INSERT INTO `midweeks` (
							`Name` ,
							`StartTime` ,
							`EndTime` ,
							`Day` ,
							`Credit` ,
							`Leader1` ,
							`Leader2` ,
							`MaxPeople` ,
							`NumSignedUp` ,
							`Location` ,
							`MeetingPlace` ,
							`SName` ,
							`Closed`
							)
							VALUES (
							'%s_%s_%s', '%s:%s', '%s:%s', '%s', '%s', '%s', '%s', '%s', '0', '%s', '%s', '%s', '0'
							);", mysql_real_escape_string($midweek_name_new), mysql_real_escape_string($midweek_day), mysql_real_escape_string($midweek_start_hr), mysql_real_escape_string($midweek_start_hr), mysql_real_escape_string($midweek_start_min), mysql_real_escape_string($midweek_end_hr), mysql_real_escape_string($midweek_end_min), mysql_real_escape_string($midweek_day), mysql_real_escape_string($midweek_credit), $midweek_leader1 == 'none' ? null : mysql_real_escape_string($midweek_leader1), $midweek_leader2 == 'none' ? null : mysql_real_escape_string($midweek_leader2), mysql_real_escape_string($midweek_max_slots), mysql_real_escape_string($midweek_location), mysql_real_escape_string($midweek_meeting), mysql_real_escape_string($midweek_name_show));
			$result = mysql_query($sql);
			if(!$result){
				$error = 'There was an error in your query, perhaps that midweek at that same time already exists';
				$sql = 'ROLLBACK;';
				$result = mysql_query($sql);
			} else {
				if($midweek_leader1 != null){
					$sql = sprintf("UPDATE `users` SET `ML` = '1' WHERE CONVERT( `users`.`ID` USING utf8 ) = '%s' LIMIT 1 ;", mysql_real_escape_string($midweek_leader1));
					$result = mysql_query($sql);
				}
				if($midweek_leader2 != null){
					$sql = sprintf("UPDATE `users` SET `ML` = '1' WHERE CONVERT( `users`.`ID` USING utf8 ) = '%s' LIMIT 1 ;", mysql_real_escape_string($midweek_leader2));
					$result = mysql_query($sql);
				}
				$sql = 'Commit;';
				$result = mysql_query($sql);
				echo "The midweek has been added! Brothers and pledges can now access it through the calendar.";
			}
		}
		if($error ==1){
			echo "<center><div style='width:400; color:red;'>".$message."</br></div></center>";
		}
	}else {
		echo "<center><div style='width:500;'><div>The system encountered some errors when processing your request, please go back and fix them to continue with your request:</div><div style='color:red;'>".$message1."</br>".$message2."</br>".$message3."</div></div></center>";
	}
	
	//check to make sure the start time is earlier then the stop time
	
	require("footer.php");
	footer();
?>